package core;

import java.util.HashMap;
import request.GameRequest;

/**
 * When server side needs to handle request received,
 * this table will provide reference to generate the
 * proper request classes to handle the input data
 * package
 *
 * @author eric, yiyi, genki, Devang, Zoran, Jason, Gary
 */
public class GameRequestTable {

    // Mapping of names of requests to corresponding class name.
    private static HashMap<Short, Class> requestNames;

    // Sets up GameRequest table.
    public static void init() {
        requestNames = new HashMap<Short, Class>();

        // First var is an int (0...65535)
        // Second var is a class name.
        try {
            add(Constants.CMSG_HEART, "RequestHeartbeat");
            add(Constants.CMSG_AUTH, "RequestLogin");
            add(Constants.CMSG_DISCONNECT, "RequestLogout");
            /*man-
            add(Constants.CMSG_PARTY_CHAT, "RequestChatParty");
             */

            add(Constants.CMSG_GLOBAL_CHAT, "RequestChatGlobal");
            add(Constants.CMSG_PRIVATE_CHAT, "RequestChatPrivate");
            add(Constants.CMSG_PUBLIC_CHAT, "RequestChatPublic");
            add(Constants.CMSG_GET_PRIVATE_CHAT, "RequestUpdateChatPrivate");
            add(Constants.CMSG_BUDDIES, "RequestBuddies");
            add(Constants.CMSG_BUDDY_INVITE, "RequestBuddyInvite");
            add(Constants.CMSG_BUDDY_ADD, "RequestBuddyAccept");
            add(Constants.CMSG_BUDDY_REMOVE, "RequestBuddyRemove");
            add(Constants.CMSG_GET_BUDDY_INVITES, "RequestUpdateBuddyInvites");
             

             /* man-
            add(Constants.CMSG_ITEM_USE, "RequestItemUse");
            add(Constants.CMSG_EQUIPMENTS, "RequestEquipments");
            add(Constants.CMSG_EQUIPMENT_REMOVE, "RequestEquipmentRemove");
            add(Constants.CMSG_MOVE, "RequestMove");
            add(Constants.CMSG_NPC_TALK, "RequestNPCTalk");
            add(Constants.CMSG_NPC_BUY, "RequestNPCBuy");
            add(Constants.CMSG_NPC_SELL, "RequestNPCSell");
            add(Constants.CMSG_ANSWER, "RequestAnswer");
            add(Constants.CMSG_HOTKEYS, "RequestHotkeys");
            add(Constants.CMSG_HOTKEY_ADD, "RequestHotkeyAdd");
            add(Constants.CMSG_HOTKEY_SET, "RequestHotkeySet");
            add(Constants.CMSG_HOTKEY_REMOVE, "RequestHotkeyRemove");
            add(Constants.CMSG_REGISTER, "RequestRegister");
            add(Constants.CMSG_BUGS, "RequestBugs");
            add(Constants.CMSG_MAP, "RequestMap");
            add(Constants.CMSG_PARTY, "RequestParty");
            add(Constants.CMSG_PARTY_ADD, "RequestPartyAdd");
            add(Constants.CMSG_PARTY_REMOVE, "RequestPartyRemove");
            add(Constants.CMSG_PARTY_ACCEPT, "RequestPartyAccept");
            add(Constants.CMSG_PARTY_LEADER, "RequestPartyLeader");
            add(Constants.CMSG_PARTY_SHARE, "RequestPartyShare");
            add(Constants.CMSG_REGISTER_BUG, "RequestRegisterBug");
            add(Constants.CMSG_BATTLE_ADD, "RequestBattleAdd");
            add(Constants.CMSG_BATTLE_END, "RequestBattleEnd");
            add(Constants.CMSG_ATTACK, "RequestAttack");
            add(Constants.CMSG_INVENTORY, "RequestInventory");
            add(Constants.CMSG_INVENTORY_REMOVE, "RequestInventoryRemove");
            add(Constants.CMSG_INVENTORY_SET, "RequestInventorySet");
            add(Constants.CMSG_JUMP, "RequestJump");
            add(Constants.CMSG_MAP_CHANGE, "RequestMapChange");
            add(Constants.CMSG_RESPAWN, "RequestRespawn");
            add(Constants.CMSG_PARTY_DESTROY, "RequestPartyDestroy");
            add(Constants.CMSG_BATTLE_START, "RequestBattleStart");
            add(Constants.CMSG_BATTLE_SET, "RequestBattleSet");
            add(Constants.CMSG_BOARD_DICE, "RequestBoardDice");
            add(Constants.CMSG_MODE_END, "RequestModeEnd");*/
            add(Constants.CMSG_BOARD_ANSWER, "RequestBoardAnswer");
            add(Constants.CMSG_BOARD_QUESTION, "RequestBoardQuestion");
            /*
            add(Constants.CMSG_MODE_READY, "RequestModeReady");
            add(Constants.CMSG_MODE_LEAVE, "RequestModeLeave");
            add(Constants.CMSG_BOARD_MOVE_SET, "RequestBoardMoveSet");
            add(Constants.CMSG_TRADE, "RequestTrade");
            add(Constants.CMSG_TRADE_ACCEPT, "RequestTradeAccept");
            add(Constants.CMSG_TRADE_CONFIRM, "RequestTradeConfirm");
            add(Constants.CMSG_TRADE_SET, "RequestTradeSet");
            add(Constants.CMSG_TRADE_REMOVE, "RequestTradeRemove");
            add(Constants.CMSG_QUESTION_COMMENT, "RequestQuestionComment");
            add(Constants.CMSG_DUEL, "RequestDuel");
            add(Constants.CMSG_DUEL_ACCEPT, "RequestDuelAccept");
            add(Constants.CMSG_DUEL_TOPICS, "RequestDuelTopics");
            add(Constants.CMSG_DUEL_ANSWER, "RequestDuelAnswer");
            add(Constants.CMSG_TARGET_SET, "RequestTargetSet");
            add(Constants.CMSG_TARGET_REMOVE, "RequestTargetRemove");*/
            add(Constants.CMSG_QUESTION_LOG, "RequestQuestionLog");
            add(Constants.CMSG_LOCAL_PLAYERS, "RequestLocalPlayers");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Adds a game request
     * @param c first int value 0..65535 of the request
     * @param req the GameRequest child class
     * @throws ClassNotFoundException
     */
    private static void add(short code, String name) throws ClassNotFoundException {
        requestNames.put(code, Class.forName("request." + name));
    }

    /**
     * Gets the name of the class corresponding with
     * a GameRequest
     *
     * @param GameRequest name
     * @return class name
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static GameRequest get(short request_id) {
        GameRequest request = null;

        try {
            Class name = requestNames.get(request_id);

            if (name != null) {
                request = (GameRequest) name.newInstance();
            } else {
                System.out.println("Invalid Request Code: " + request_id);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return request;
    }
}

